package com.xpgk.xpsoft.system;

import com.jfinal.aop.Before;
import com.jfinal.kit.HashKit;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.xpgk.common.controller.BaseController;
import com.xpgk.common.kit.EncryptKit;
import com.xpgk.common.kit.PubKit;
import com.xpgk.xpsoft.constants.XPSoftConstant;
import com.xpgk.xpsoft.user.UserService;

public class SystemController extends BaseController {

	public void index() {
		keepPara();
		Kv cond = Kv.by("type = ", getPara("type")).set("name like ", isParaBlank("name") ? null : getPara("name", "") + "%");
		Page<Record> pages = Db.paginate(getParaToInt("pageNum", 1), 10, Db.getSqlPara("xpSoftParam.findSystemParamList", Kv.by("cond", cond)));
		// List<Record> typeList=paramTypeService.getTypeList();
		// Map<Integer, String> map=PubKit.toMap(typeList, "type", "note");
		// PubKit.addColumnTo(pages.getList(), "type", map, "typeName");
		setAttr("lists", pages);
		// setAttr("paramTypeList", typeList);
		setAttr("bCanAdd", UserService.hasRight(getCurUserID(), "/xpSystem/add_ui"));
		setAttr("bCanEdit", UserService.hasRight(getCurUserID(), "/xpSystem/edit_ui"));
		render("param/index.html");
		// t.OutputDelta();
	}

	// 注：定义函数名时，如"void add()",页面的展示请以_ui结尾，void add_ui() 涉及到权限问题
	// add与add_ui有同样的权限
	public void add_ui() {
		Record param = new Record().set("systemID", XPSoftConstant.systemID);
		setAttr("param", param.getColumns());
		render("param/add.html");
	}

	// 注册
	@Before(Tx.class)
	public void add() {
		keepPara();
		Record param = GetRecord("param");
		String result = SystemService.saveParam(param);
		if (!result.equals("ok"))
			renderMsg(result, "param/add.html");
		else
			renderMsg("添加成功.", "param/add.html", true);
		// add_ui();
	}

	// 修改
	public void edit_ui() {
		int id = getParaToInt("id", 0);
		Record param = Db.findById("frm_system_param", id);
		setAttr("param", param.getColumns());
		setAttr("bEdit", 1);
		render("param/edit.html");
	}

	// 修改
	@Before(Tx.class)
	public void edit() {
		Record param = GetRecord("param");
		keepPara();
		setAttr("bEdit", 1);
		String result = SystemService.updateParam(param);
		if (!result.equals("ok")) {
			renderMsg(result, "param/edit.html");
			return;
		}
		renderMsg("修改成功.", "param/edit.html", true);
	}

	// 修改
	public void init_ui() {
		Record system = SystemService.GetSystem();
		if (system != null)
			setAttr("system", system.getColumns());
		render("init/edit.html");
	}

	// 修改
	@Before(Tx.class)
	public void init() {
		Record system = GetRecord("system");
		keepPara();
		String result = SystemService.updateSystem(system);
		if (!result.equals("ok")) {
			renderMsg(result, "init/edit.html");
			return;
		}
		setAttr("system", system);
		renderMsg("设置成功.", "init/edit.html", true);
	}

	// 调用记录
	public void log() {
		if (getPara("name") != null) {
			keepPara();
			Kv cond = Kv.by("l.name = ", getPara("name", "")).set("u.userName = ", getPara("userName", "")).set("l.lx1 = ", getPara("type1").equals(" ") ? "" : getPara("type1"));
			cond.set(" l.ip = ", getPara("ip", "")).set("l.gxsj >= ", (PubKit.toDate(getPara("beginTime", PubKit.Date()), null))).set("l.gxsj <= ", PubKit.toDate(getPara("endTime"), null));
			Page<Record> pages = Db.paginate(getParaToInt("pageNum", 1), 10, Db.getSqlPara("xpSoftParam.findLogList", Kv.by("cond", cond)));
			setAttr("lists", pages);
		}
		render("log/index.html");
	}

	public void convertParam() {
		String pwd = getPara("pwd");
		String param = getPara("param");
		if (PubKit.hasBlank(pwd, param) || !pwd.equals(HashKit.md5("127.0.0.1"))) {
			render("param/convertparam.html");
			return;
		}
		setAttr("param", EncryptKit.encrypt(param));
		render("param/convertparam.html");
	}
}
